package stat;

import java.util.ArrayList;
import java.util.Map;

public class MeanLatency {
	/**
	 * @param args
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
//		String file = "scripts/gnuplot/logs/stat_rarest_first.log";
//		String file = "scripts/gnuplot/logs/stat_sliding_window.log";
		String file = "scripts/gnuplot/logs/stat_segments.log";

		//read log
		Map map = LogReader.getMap(file);

		float res = 0;
		int nrPeers = map.size();
		for(Object peer : map.keySet()) { //each peer
			ArrayList<Integer> goal = new ArrayList<Integer>();
			for(int n=0; n<20; n++) {
				goal.add(n);
			}
			
			ArrayList<Integer> alreadySeen = new ArrayList<Integer>(); 
			
			ArrayList<LogReaderLine> lines = (ArrayList<LogReaderLine>) map.get((String)peer);
			
			boolean firstLine = true;
			long firstVal = 0;
			for(LogReaderLine lrl : lines) { //each line for the peer
				int chunk = lrl.getPiece();
				
				if(firstLine) {
					firstVal = lrl.getTimestamp();
					firstLine = false;
				}
				
				if(chunk >= 0 && chunk < 20) {
					alreadySeen.add(chunk);
				}
				
				
				if(alreadySeen.containsAll(goal)) {
					res += lrl.getTimestamp() - firstVal;
					System.out.println("-----------------");
					break;
				}
			}
		}

		
		System.out.println("Latency: " + (res/nrPeers)/1000);
	}
}
